package file;

import hypercast.I_LogicalAddress;
import intf.RunOSXcmd;

import java.io.*;
import ctrl.Control;

public class WebShare {

	int mode = 0;
	Control gui;
	File targetFile;
	I_LogicalAddress dest;

	public WebShare(Control g, String path, I_LogicalAddress d) {
		gui = g;
		targetFile = new File(path);
		dest = d;

	}

	public void action() {
		(new Thread(new sendFile())).start();
	}

	private class sendFile implements Runnable {
		public void run() {
			byte[] data = null;
			try {
				data = org.apache.commons.io.FileUtils.readFileToByteArray(targetFile);
			} catch (IOException e) {
				System.out.println("@WebShare - IOException: readFileToByteArray error");
			}
			gui.getNet().sendBytes(data, 6);
			targetFile.delete();
			//TODO: need to delete file after sending
			
			/*String OS = System.getProperty("os.name").toLowerCase();
			if (OS.indexOf("mac") >= 0) {
				String cmd = "rm " + targetFile.getName();
				RunOSXcmd.run(cmd);
			}*/
			
		}
	}
}
